<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Hanson\Vbot\Foundation\Vbot;
use App\Classes\WechatRobot\WechatRobot;

class WechatRobotCommand extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'wechat-robot:action {action}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Wechat-Robot Command';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $action = $this->argument('action');
        switch ($action) {
            case 'start':
                $this->start();
                break;

            case 'stop':
                $this->stop();
                break;

            case 'restart':
                $this->restart();
                break;
        }
    }

    /**
     * [start description]
     * @return [type] [description]
     */
    private function start()
    {
        echo 'start...';

        $wechatRobot = new WechatRobot();
        $wechatRobot->run();

        // $config = config('wechat-robot');
        // $vbot = new Vbot($config);
        // $vbot->messageHandler->setHandler(function(Collection $message){
        //     Text::send($message['from']['UserName'], 'hi');
        // });
        // $vbot->server->serve();

        echo 'start completion';
    }

    /**
     * [stop description]
     * @return [type] [description]
     */
    private function stop()
    {
        echo 'stop';
    }

    /**
     * [restart description]
     * @return [type] [description]
     */
    private function restart()
    {
        echo 'restart';
    }
}
// 1.执行 ps -aux|grep artisan命令，获取pid（有多个进程，杀第一个即可）
// 2.执行 kill pid命令，pid是第一步你获取的
// 3.如果想后台值守，一定加上nohup命令！！！